home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / util / cli / Randomizer.lha / Random / Randomizer.c < prev    next >
C/C++ Source or Header  |  1997-07-16  |  2KB  |  87 lines

  1. /*
  2.  * Random numbers from 0 to system time ;^)
  3.  *
  4.  * You will need to change the includes for any other compiler than Maxon C
  5.  */
  6.  
  7. #include <pragma/exec_lib.h>
  8. #include <pragma/dos_lib.h>
  9. #include <pragma/intuition_lib.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12.  
  13.  
  14. struct Library *IntuitionBase = NULL;
  15. LONG args[2] = {0, 0};
  16. STRPTR tmplate = "HI=HIGH/K/N,LO=LOW/K/N\n";
  17. char *verstring = "$VER: Randomizer 1.1 (16.07.97)";
  18.  
  19.  
  20. /* If you use the Maxon C/C++ compiler, this program effectively does nothing,
  21.    if you start it from workbench. */
  22.  
  23. void main(void)
  24. {
  25.     register LONG low = 0, high = 1, random, *num;
  26.     struct RDArgs *rdargs;
  27.     ULONG secs, mics;
  28.     int rw = RETURN_FAIL;
  29.     char numbuf[12];
  30.  
  31.     IntuitionBase = OpenLibrary("intuition.library", 37);
  32.     if(IntuitionBase)
  33.     {
  34.         rw = RETURN_ERROR;
  35.  
  36.         rdargs = ReadArgs(tmplate, args, NULL);
  37.         if(rdargs)
  38.         {
  39.             num = (LONG *) args[1];
  40.             if(num)
  41.             {
  42.                 low = *num;
  43.             }
  44.  
  45.             num = (LONG *) args[0];
  46.             if(num)
  47.             {
  48.                 high = *num;
  49.             }
  50.  
  51.             FreeArgs(rdargs);
  52.         }
  53.  
  54.         CurrentTime(&secs, &mics);
  55.         CloseLibrary(IntuitionBase);
  56.  
  57.         if(low >= high)
  58.         {
  59.             PrintFault(ERROR_BAD_NUMBER, "LOW >= HIGH !!! ");
  60.             exit(rw);
  61.         }
  62.  
  63.         // Create random-range
  64.         high -= low;
  65.         high++;
  66.  
  67.         // Create random number: range plus lower boundary
  68.         secs += mics;
  69.         secs %= high;
  70.         random = secs + low;
  71.  
  72.         /* I am using my own sprintf() [utilizing RawDoFmt()], so your code
  73.            will most likely be much bigger than mine. It is a safety kludge
  74.            to distinguish a fake from the original. For your inconvenience,
  75.            of course. You can find a smaller one in the autodoc text to
  76.            exec.library/RawDoFmt(). */
  77.         sprintf(numbuf, "%ld", random);
  78.  
  79.         if(SetVar("RANDOM", numbuf, -1, LV_VAR))
  80.         {
  81.             rw = RETURN_OK;
  82.         }
  83.     }
  84.  
  85.     exit(rw);
  86. }
  87.